package com.me.launcher.tools;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Window;

/**
 * @author Administrator
 * @date 2017/1/19 0019
 */
public class ScreenUtils {
    private static int screenW;
    private static int screenH;
    private static float screenDensity;
    private static float screenScale;
    private static Window mWindow;

    private static class Holder {
        private static final ScreenUtils INSTANCE = new ScreenUtils();
    }

    private ScreenUtils() {
    }

    public static final ScreenUtils getInstance() {
        return Holder.INSTANCE;
    }


    public void initScreen(Activity mActivity) {
        DisplayMetrics metric = new DisplayMetrics();
        mWindow = mActivity.getWindow();
        mActivity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        screenW = metric.widthPixels;
        screenH = metric.heightPixels;
        screenDensity = metric.density;
        screenScale = metric.scaledDensity;
    }

    public int getScreenW() {
        return screenW;
    }

    public int getScreenH() {
        return screenH;
    }

    public float getScreenDensity() {
        return screenDensity;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public int dp2px(float dpValue) {
        return (int) (dpValue * getScreenDensity() + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public int px2dp(float pxValue) {
        return (int) (pxValue / getScreenDensity() + 0.5f);
    }


    public int px2sp(float pxValue) {
        return (int) (pxValue / screenScale + 0.5f);
    }


    public int sp2px(float spValue) {
        return (int) (spValue * screenScale + 0.5f);
    }


    /**
     * statusBar高度
     * @return 资源ID
     **/
    public int getStatusBarHeight() {
        int resourceId = 0;
        try {
            Class<?> clazz = Class.forName("com.android.internal.R$dimen");
            Object object = clazz.newInstance();
            resourceId = Integer.parseInt(clazz.getField("status_bar_height")
                    .get(object).toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resourceId;

    }

}

